﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GeneCMS.Models.physical;
using System.Text;

namespace GeneCMS.Controllers
{
    public class PageViewModel
    {
        public int ID { get; set; }
        public int? ParentPageID { get; set; }
        public string Title { get; set; }
        public int Position { get; set; }
        public string Url { get; set; }
        public string Folder { get; set; }
        public bool InMenu { get; set; }
        public string Keywords { get; set; }
        public string Description { get; set; }

        public PageViewModel()
        {
        }

        //public PageViewModel(Page page)
        //{
        //    ID = page.ID;
        //    Title = page.Title;
        //    Position = page.Position;
        //    ParentPageID = page.ParentPageID;
        //    Url = page.Url;
        //}
    }

    public class WebsiteListViewModel
    {
        public Dictionary<string, List<PageViewModel>> ChildPages { get; set; }
        public List<PageViewModel> TopLevelPages { get; set; }
        public int WebsiteID { get; set; }

        public WebsiteListViewModel(int website_id)
        {
            WebsiteID = website_id;

            CMSDBDataContext db = new CMSDBDataContext();
            ChildPages = new Dictionary<string, List<PageViewModel>>();

            var pages = (from page_rows in db.Pages where page_rows.WebsiteID == website_id 
                         orderby page_rows.ParentPageID, page_rows.Position
                         select new PageViewModel()
                         {
                             ID = page_rows.ID,
                             Title = page_rows.Title,
                             Position = page_rows.Position,
                             ParentPageID = page_rows.ParentPageID,
                             Url = page_rows.Url,
                             Folder = page_rows.Folder,
                             InMenu = page_rows.InMenu,
                             Keywords = page_rows.Keywords,
                             Description = page_rows.Description

                         }
                         ).ToList();
            //string sql = pages.ToString();
            string key = "0";
            foreach (var page in pages)
            {
                key = (page.ParentPageID ?? -1).ToString();
                if (!ChildPages.Keys.Contains(key))
                {
                    ChildPages.Add(key, new List<PageViewModel>());
                }
                ChildPages[key].Add(page);
            }
            if (ChildPages.ContainsKey("-1"))
            {
                TopLevelPages = ChildPages["-1"];
            }
            else
            {
                TopLevelPages = new List<PageViewModel>();
            }
        }

        //public string ToHtmlList()
        //{
        //    StringBuilder html = new StringBuilder("<ol>");
        //    foreach (PageViewModel page in TopLevelPages)
        //    {
        //        html.Append(FormatPageItem(page));
        //    }
        //    html.Append("</ol>");
        //    return html.ToString();
        //}

        //public string FormatPageItem(PageViewModel page)
        //{
        //    StringBuilder html = new StringBuilder("<li>");
        //    html.Append(page.Title);
        //    if (ChildPages.Keys.Contains(page.ID))
        //    {
        //        html.Append("<ol>");
        //        foreach (PageViewModel child_page in ChildPages[page.ID])
        //        {
        //            html.Append(FormatPageItem(child_page));
        //        }
        //        html.Append("</ol>");
        //    }
        //    html.Append("</li>");
        //    return html.ToString();
        //}
    }
}
